package com.dao;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.common.Enums;
import com.jdbc.DBUtil;

import java.util.List;

public class AdminDao {
    //用户登录
    public AdminInfo login(String adminName, String password) {
        String sql="select * from adminInfo where adminName=? and password=? and state=?";
        return DBUtil.getSingleObject(sql, AdminInfo.class,adminName,password, Enums.EmnuAdminState.正常.getValue());
    }
    //添加用户
    public int addAdmin(AdminInfo admin) {
        String sql="insert into adminInfo (note, password,adminName,state,roleId) values (?,?,?,?,?) ";
        return DBUtil.update(sql,admin.getNote(),admin.getPassword(),admin.getAdminName(),admin.getState(),admin.getRoleId());
    }
    //查询所有的用户名  判断用户名重复
    public List<AdminInfo> getAdminName() {
        String sql="select adminName from adminInfo ";
        return DBUtil.getList(sql,AdminInfo.class);
    }
    //得到所有的未被删除的用户个数
    public int getAdminCount() {
        long count= DBUtil.getScalar("select count(*) from adminInfo where state!=0") ;
        return Integer.parseInt(count+"");
    }
    //得到所有的未被删除的用户
    public List<AdminInfo> getAdminList(PageInfo page) {
        String sql="select * from adminInfo where state!=0 limit ?,?";
        return DBUtil.getList(sql, AdminInfo.class,page.getBeginRow(),page.getPageSize());
    }
    //锁定用户
    public void lock(Integer id) {
        String sql = "update adminInfo set state=? where id=? ";
        DBUtil.update(sql,Enums.EmnuAdminState.锁定.getValue(),id);
    }
    //解锁用户
    public void unlock(Integer id) {
        String sql = "update adminInfo set state=? where id=? ";
        DBUtil.update(sql,Enums.EmnuAdminState.正常.getValue(), id);
    }
    //删除用户
    public void del(Integer id) {
        String sql="update adminInfo set state=? where id=?";
        DBUtil.update(sql,Enums.EmnuAdminState.删除.getValue(),id);
    }

    //删除多个用户
    public void delAdmin(String[] ids) {
        String sql = "update adminInfo set state=0 where id in(";
        for(int i=0;i<ids.length;i++) {
            if(i<ids.length-1) {
                sql+=ids[i] + ",";
            }else {
                sql+=ids[i] + ")";
            }
        }
        DBUtil.update(sql);
    }
    //通过Id得到用户
    public AdminInfo getAdminById(Integer id) {
        String sql="select * from adminInfo where id=?";
        return DBUtil.getSingleObject(sql,AdminInfo.class,id);
    }
    //更新用户
    public Integer updateAdmin(AdminInfo admin) {
        String sql="update adminInfo set adminName=?,password=?,note=? where id=?";
        return DBUtil.update(sql,admin.getAdminName(),admin.getPassword(),admin.getNote(),admin.getId());
    }
    //更改密码
    public int updatePassword(AdminInfo admin) {
        String sql="update adminInfo set password=? where id=?";
        return DBUtil.update(sql,admin.getPassword(),admin.getId());
    }
    //查询所有的用户角色
    public List<AdminInfo> getAllAdmin(PageInfo page) {
        String sql="select a.id,adminName,state,roleName from adminInfo a left join roleInfo b on b.id=a.roleId where state=1 limit ?,?";
        return  DBUtil.getList(sql,AdminInfo.class,page.getBeginRow(),page.getPageSize());
    }
    //更新用户角色
    public void updateAdminRole(Integer adminId, Integer roleId) {
        String sql="update adminInfo set roleId=? where id=?";
        DBUtil.update(sql,roleId,adminId);
    }
}
